<?php

namespace app\admin\model\facrm\contract;

use think\Model;


class Plan extends Model
{

    

    

    // 表名
    protected $name = 'facrm_contract_plan';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = "create_time";
    protected $updateTime = "update_time";
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'plan_time_text',
        'create_time_text',
        'update_time_text'
    ];
    

    



    public function getPlanTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['plan_time']) ? $data['plan_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPlanTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function contract()
    {
        return $this->belongsTo('\app\admin\model\facrm\Contract', 'contract_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function customer()
    {
        return $this->belongsTo('app\admin\model\facrm\Customer', 'Customer_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function getStatusList()
    {
        //1正常2不能再续费或新购0关闭
        return ['2' => __('已回款'),'0' => __('进行中'),'-1'=>"到期未回款"];
    }

}
